package cn.max.controllers;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import cn.incontent.cda.client.common.ArgumentList;
import cn.incontent.cda.client.utils.EncodingUtil;

/**
 * @author Val.(Valentine Vincent) E-mail:valer@126.com
 * @version 1.0
 * @date 2013-12-8 Instruction :
 **/
public class ModuleContext {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ArgumentList args;
	private List<FileItem> files;
	
	public ModuleContext(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public ArgumentList getArgs() {
		setupArgAndFiles();
		return args;
	}

	public List<FileItem> getFiles() {
		setupArgAndFiles();
		return files;
	}

	public String getParam(String key) {
		setupArgAndFiles();
		return args.get(key);
	}
	
	@SuppressWarnings("unchecked")
	private void setupArgAndFiles() {
		
		if (args != null && files != null) {
			return;
		}
		
		args = new ArgumentList();
		files = new ArrayList<FileItem>();
		
		Enumeration<String> keys = request.getParameterNames();
		
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			
			try {
				args.add(key, URLDecoder.decode(request.getParameter(key), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		
		FileItemFactory factory = new DiskFileItemFactory();
		List<FileItem> items;
		try {
			items = new ServletFileUpload(factory).parseRequest(request);
			
			for (FileItem fi : items) {
				
				if (fi.isFormField()) {
					args.add(fi.getFieldName(), fi.getString("UTF-8"));
				} else {
					files.add(fi);
				}
				
			}
		} catch (FileUploadException e) {
			//
			try {
				String s = EncodingUtil.getStringFromStream(request.getInputStream(), "UTF-8");
				
				for (String kv : s.split("&")) {
					
					if (kv == null || kv.length() == 0) {
						continue;
					}
					
					String[] kvArr = kv.split("=");
					
					if (kvArr.length != 2) {
						continue;
					}
					
					args.add(kvArr[0], URLDecoder.decode(kvArr[1], "UTF-8"));
				}
				
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
	}

}
